//
//  VideoReplyDetailController.swift
//  maopu
//
//  Created by 黄海波 on 2017/4/19.
//  Copyright © 2017年 maopu. All rights reserved.
// 视频回复详情页

import UIKit

class VideoReplyDetailController: BaseViewController , UITextViewDelegate{
    fileprivate var tableView:UITableView!
    fileprivate var hotHeaderView:UIView!
    fileprivate var hotHeaderTitleLb:UILabel!
    
    fileprivate var newHeaderView:UIView!
    fileprivate var newHeaderTitleLb:UILabel!
    
    fileprivate var bottomView:SendCommentView!
    fileprivate var moreToolView:MoreToolView!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.initView()
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
    }
    
    override func viewWillDisappear(_ animated: Bool) {
        super.viewWillDisappear(animated)
        NotificationCenter.default.removeObserver(self)
    }
    
    fileprivate func initView(){
        self.automaticallyAdjustsScrollViewInsets = false
        self.navigationItem.title = "视频详情"
        self.navigationItem.leftBarButtonItem = UIBarButtonItem.setBackBarButton(action: #selector(self.goBack), target: self)
        self.navigationItem.rightBarButtonItem = UIBarButtonItem.setImgBarButton(imgName: "more_ico", action: #selector(self.showShareView), target: self)
        
        self.tableView = UITableView.init(frame: CGRect.init(x: 0, y: 64, width: AppWidth, height: AppHeight - 64 - 50), style:UITableViewStyle.grouped)
        self.tableView.estimatedRowHeight = 300
        self.tableView.rowHeight = UITableViewAutomaticDimension
        self.tableView.separatorStyle = .none
        self.tableView.keyboardDismissMode = .onDrag
        self.tableView.tableHeaderView = UIView.init(frame: CGRect.init(x: 0, y: 0, width: AppWidth, height: 0.01))
        self.tableView.delegate = self
        self.tableView.dataSource = self
        self.tableView.register(UINib(nibName: "VideoListItemCell", bundle: nil), forCellReuseIdentifier: "VideoListItemCell")
        self.tableView.register(UINib(nibName: "ReplyItemCell", bundle: nil), forCellReuseIdentifier: "ReplyItemCell")
        self.view.addSubview(self.tableView)
        
        self.initHeaderView()
        self.initBottomView()
    }
    
    fileprivate func initHeaderView(){
        self.hotHeaderView = UIView.init(frame: CGRect(x: 0, y: 0, width: AppWidth, height: 46))
        self.hotHeaderView.backgroundColor = UIColor.white
        
        let lineView = UIView(frame: CGRect(x: 0, y: 0, width: AppWidth, height: 10))
        lineView.backgroundColor = UIColor.m_viewBackgroundColor()
        self.hotHeaderView.addSubview(lineView)
        
        self.hotHeaderTitleLb = UILabel(frame: CGRect(x: 20, y: 20, width: AppWidth - 40, height: 16))
        self.hotHeaderTitleLb.font = UIFont.RegularDefaultFont(16)
        self.hotHeaderTitleLb.textColor = UIColor.m_textDeepColor()
        self.hotHeaderTitleLb.text = "热门回复"
        self.hotHeaderView.addSubview(self.hotHeaderTitleLb)
        
        self.newHeaderView = UIView.init(frame: CGRect(x: 0, y: 0, width: AppWidth, height: 46))
        self.newHeaderView.backgroundColor = UIColor.white
        
        let newLineView = UIView(frame: CGRect(x: 0, y: 0, width: AppWidth, height: 10))
        newLineView.backgroundColor = UIColor.m_viewBackgroundColor()
        self.newHeaderView.addSubview(newLineView)
        
        self.newHeaderTitleLb = UILabel(frame: CGRect(x: 20, y: 20, width: AppWidth - 40, height: 16))
        self.newHeaderTitleLb.font = UIFont.RegularDefaultFont(16)
        self.newHeaderTitleLb.textColor = UIColor.m_textDeepColor()
        self.newHeaderTitleLb.text = "最新回复"
        self.newHeaderView.addSubview(self.newHeaderTitleLb)
    }
    
    fileprivate func initBottomView(){
        self.bottomView = SendCommentView(frame: CGRect(x: 0, y: AppHeight - 50, width: AppWidth, height: 50))
        self.bottomView.backgroundColor = UIColor.white
        self.view.addSubview(self.bottomView)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
    }
    
    //MARK:查看更多
    @objc fileprivate func showShareView(){
        if self.moreToolView == nil{
            self.moreToolView = MoreToolView.init(isVideo: true, onlyShowShare: false)
        }
        self.moreToolView.show()
    }
}

extension VideoReplyDetailController:UITableViewDelegate , UITableViewDataSource{
    func numberOfSections(in tableView: UITableView) -> Int {
        return 3
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if section == 0{
            return 1
        }else if section == 1{
            return 2
        }
        return 3
    }
    
    func tableView(_ tableView: UITableView, heightForHeaderInSection section: Int) -> CGFloat {
        if section == 0{
            return 0.01
        }
        return 46
    }
    
    func tableView(_ tableView: UITableView, heightForFooterInSection section: Int) -> CGFloat {
        return 0.1
    }
    
    func tableView(_ tableView: UITableView, viewForHeaderInSection section: Int) -> UIView? {
        if section == 0{
            return nil
        }else if section == 1{
            return hotHeaderView
        }
        return self.newHeaderView
    }
    
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if indexPath.section == 0{
            var cell:VideoListItemCell? = tableView.dequeueReusableCell(withIdentifier: "VideoListItemCell") as? VideoListItemCell
            if cell == nil{
                cell = VideoListItemCell(style: UITableViewCellStyle.default,reuseIdentifier:"VideoListItemCell")
            }
            cell?.selectionStyle = .none
            cell!.refreshCellData(message: "")
            return cell!
        }else{
            let cell = tableView.dequeueReusableCell(withIdentifier: "ReplyItemCell", for: indexPath) as! ReplyItemCell
            cell.selectionStyle = .none
            cell.refreshCellData(indexPathRow: indexPath.row, cellType: VideoReplyCellType.reply)
            return cell
        }
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        if indexPath.section  != 0{
            let vc = VideoCommentDetailController()
            self.navigationController?.pushViewController(vc, animated: true)
        }
    }
}
